package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView;
    private String firstNum = "";
    private String operator = "";
    private String secondNum = "";
    private String result = "";
    private String showText = "";


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_show);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_division).setOnClickListener(this);
        findViewById(R.id.btn_multiplication).setOnClickListener(this);
        findViewById(R.id.btn_restoration).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_sign).setOnClickListener(this);
        findViewById(R.id.btn_x).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_point).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String input;
        if (view.getId() == R.id.btn_sign) {
            input = "√";
        } else {
            input = ((TextView) view).getText().toString();
        }

        if (view.getId() == R.id.btn_cancel) {
            clear("");

        } else if (view.getId() == R.id.btn_restoration) {
            clear("0");

        } else if (view.getId() == R.id.btn_add || view.getId() == R.id.btn_minus || view.getId() == R.id.btn_multiplication || view.getId() == R.id.btn_division) {
            operator = input;
            refreshText(showText + operator);
        } else if (view.getId() == R.id.btn_equal) {
            double calculate = calculateFour();
            refreshOperate(String.valueOf(calculate));
            refreshText(showText + "=" + result);

        } else if (view.getId() == R.id.btn_sign) {

        } else if (view.getId() == R.id.btn_x) {

        } else {
            if (operator.equals("")) {
                firstNum = firstNum + input;
            } else {
                secondNum = secondNum + input;
            }
            if (showText.equals("0") && !input.equals(".")) {
                refreshText(input);
            } else {
                refreshText(showText + input);
            }

        }
    }

    private double calculateFour() {
        switch (operator) {
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "X":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            default:
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
    }

    private void clear(String clear) {
        refreshText(clear);
        refreshOperate("");

    }

    private void refreshOperate(String newResult) {
        result = newResult;
        firstNum = result;
        secondNum = "";
        operator = "";
    }

    private void refreshText(String test) {
        showText = test;
        textView.setText(showText);
    }
}